<script>
import { mapState } from 'vuex';
import AreaForm from '@/views/car/area/components/areaForm.vue';

export default {
  name: 'area',
  components: { AreaForm },
  data() {
    return {
      // 获取区域列表的分页参数
      params: {
        page: 1,
        pageSize: 10,
      },
      // 是否显示dialog组件
      dialogVisible: false,
      // 是否处于编辑区域模式
      edit: false,
      // areaForm组件实例
      areaFormRef: null,
      loading: false,
    };
  },
  methods: {
    // 用于派发action发送请求获取区域列表的回调函数
    async fetchAreaList() {
      try {
        this.loading = true;
        await this.$store.dispatch('areaStore/fetchAreaListAction', this.params);
      } catch (error) {
        this.$message.error(error.message);
      } finally {
        this.loading = false;
      }
    },
    // 每页pageSize发生变化的回调函数
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.fetchAreaList();
    },
    // 当前页发生变化的回调函数
    handleCurrentChange(page) {
      this.params.page = page;
      this.fetchAreaList();
    },
    // 点击删除按钮的回调函数
    async handleDelete(card) {
      const confirm = await this.$confirm(`此操作将永久删除${card.areaName}, 是否继续?`, '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).catch(error => error);
      if (confirm === 'cancel') return;
      try {
        const deleteResult = await this.$store.dispatch('areaStore/deleteCardAction', card.id);
        await this.fetchAreaList();
        this.$message.success(deleteResult);
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    // 点击添加的按钮
    handleAdd() {
      this.dialogVisible = true;
      this.edit = false;
    },
    // 点击编辑的按钮
    handleEdit(row) {
      this.dialogVisible = true;
      this.edit = true;
      this.$nextTick(() => {
        this.$refs.areaFormRef.fetchAreaDropDetail(row);
      });
    },
    // dialog组件关闭的回调函数
    closeDialog() {
      this.$refs.areaFormRef.clearFormData();
    },
    // 点击确认按钮的回调函数
    async handleSubmit() {
      const validate = await this.$refs.areaFormRef.$refs.formRef.validate().catch(error => error);
      if (!validate) return this.$message.warning('请完善表单信息');
      let result;
      try {
        if (this.edit) {
          result = await this.$store.dispatch('areaStore/updateAreaDropAction', this.$refs.areaFormRef.formData);
        } else {
          result = await this.$store.dispatch('areaStore/addAreaDropAction', this.$refs.areaFormRef.formData);
        }
        this.dialogVisible = false;
        this.fetchAreaList();
        this.$message.success(result);
      } catch (error) {
        this.$message.error(error.message);
      }

      /*      if (this.edit) {
              try {
                const updateAreaDropResult = await this.$store.dispatch('areaStore/updateAreaDropAction', this.$refs.areaFormRef.formData);
                this.dialogVisible = false;
                this.fetchAreaList();
                this.$message.success(updateAreaDropResult);
              } catch (error) {
                this.$message.error(error.message);
              }
            } else {
              try {
                const addAreaDropResult = await this.$store.dispatch('areaStore/addAreaDropAction', this.$refs.areaFormRef.formData);
                this.dialogVisible = false;
                this.fetchAreaList();
                this.$message.success(addAreaDropResult);
              } catch (error) {
                this.$message.error(error.message);
              }
            }
     */
    },
  },
  computed: {
    ...mapState('areaStore', ['areaList', 'total']),
    title() {
      return this.edit ? '编辑区域' : '添加区域';
    },
  },
  mounted() {
    this.fetchAreaList();
  },
};
</script>

<template>
  <div class="area-container">
    <el-card shadow="never">
      <el-button size="small" type="primary" icon="el-icon-plus" @click="handleAdd">添加区域</el-button>
      <el-table size="small" border stripe :header-cell-style="{background:'#f4f6f8',color:'#000'}" :data="areaList" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, 1)">
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="ruleId" label="区域ID" align="center"></el-table-column>
        <el-table-column prop="areaName" label="区域名称" align="center"></el-table-column>
        <el-table-column prop="spaceNumber" label="车位数（个）" align="center"></el-table-column>
        <el-table-column prop="areaProportion" label="面积（㎡）" align="center"></el-table-column>
        <el-table-column prop="ruleName" label="计费规则" align="center"></el-table-column>
        <el-table-column prop="hoverRuleName" label="计费规则详情" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="remark" label="备注" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.page" :page-sizes="[10, 20, 30, 40]" :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
    <el-dialog :visible.sync="dialogVisible" :title="title" append-to-body @close="closeDialog">
      <area-form ref="areaFormRef"></area-form>
      <template #footer>
        <el-button size="small" @click="dialogVisible=false">取消</el-button>
        <el-button size="small" type="primary" @click="handleSubmit">确认</el-button>
      </template>
    </el-dialog>

  </div>

</template>

<style scoped lang="scss">
.area-container {
  padding: 15px;

  .el-dialog__body {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
